/**
 * 按需维保
 */
import {Component, EventEmitter, Input, Output} from "@angular/core";

import {ActivatedRoute, Router} from "@angular/router";
import { NzNotificationService} from "ng-zorro-antd";
import {HttpServcie} from "../../../../service/http.service";
import {ElevatorInfoBean} from "../../../elevator/elevator-info/bean/elevatorInfo.bean";
import {elevatorTypes} from "../../../../service/constant/elevatorTypeConstant";


@Component({
  selector: "contract-project-elevator",
  templateUrl: "contract-project-elevator.html",
  styleUrls: ["contract-project-elevator.scss"]
})

export class ContractProjectElevator {
  constructor(
    private httpService: HttpServcie,
    private router: Router,
    private _notification: NzNotificationService,
    private activatedRoute: ActivatedRoute,
  ) {

  }
  @Input() inValue: any = [];
  @Output() outValue = new EventEmitter();
  elevatorTypeId : string; //电梯类型
  list : any = []; //电梯集合
  total : any = 0;
  elevatorInfo :　ElevatorInfoBean = new ElevatorInfoBean();
  //多选框
  allChecked: any = false;
  indeterminate: any = false;
  elevatorIds : any = [];
  elevatorTypes = elevatorTypes;
  user : any;

  ngOnInit(){
    this.elevatorInfo.pageSize = 10;
    this.elevatorInfo.currentPage = 1;
    //修改获取电梯详情为调查询接口
    this.activatedRoute.params.subscribe(data => {
      if(data !=null && typeof(data) != "undefined"){
        if(data.elevatorIds){
          this.elevatorInfo.elevatorIds = data.elevatorIds.split(',');
        }
        if(data.jgPartyId){
          this.elevatorInfo.jgPartyId = data.jgPartyId;
        }
        this.query();
      }else{
        this._notification.create("error", "错误提示", "未获取到电梯参数");
        return;
      }
    });
  }



  query(){
    this.httpService.demandMaintGetElevators(this.elevatorInfo).subscribe(
      data => {
        if (data.status == 200) {
          this.list = data.data.list;
          this.total = data.data.total;
        } else if (data.status === 401) {
          this.router.navigateByUrl("/login");
          return;
        } else {
          // 异常
          this._notification.create("error", "错误提示", data.message);
        }
      },
      error => {
        console.log(JSON.stringify(error));
      }
    );
  }

  /**
   * 跳转到电梯台账页面
   */
  toDetail(data){
    this.router.navigate(["/home/elevator-detail", {elevatorId : data.id}]);
  }

  toBack(){
    window.history.back();
  }

  //table加载数据时
  displayDataChange($event) {
    this.list = $event;
    this.refreshStatus();
  }

  //重置选择框
  refreshStatus() {
    const allChecked = this.list.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.list.every(
      value => value.disabled || !value.checked
    );
    this.allChecked = allChecked;
    this.indeterminate = !allChecked && !allUnChecked;
  }

  //全选
  checkAll(value) {
    if (value) {
      this.list.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.list.forEach(data => (data.checked = false));
    }
  }

  //分页
  nzPageSizeChange(event) {
    this.elevatorInfo.pageSize = event;
    this.elevatorInfo.currentPage = 1;
    this.query();
  }

  //输入框的方法
  nzPageIndexChange(event) {
    this.elevatorInfo.currentPage = event;
    this.query();
  }

  //当前页
  nzPageIndexClickChange(event) {
    this.elevatorInfo.currentPage = event;
    this.query();
  }
  // 查询面板
  panels = {
    active: true,
    name: "查询",
    disabled: false,
    childPanel: [
      {
        active: false,
        name: "查询"
      }
    ]
  };
}
